#GUI
require 'libglade2'

require 'cairo'


class Interface
	include GetText

	attr :glade

	@@STATE_MOVE = 1
	@@GUI_SCALE = 15
	
	def gameBoardClick(w, e)
	
		x = e.x
		y = e.y
		
		scaledX = (x / @@GUI_SCALE).to_i
		scaledY = (y / @@GUI_SCALE).to_i
		
		region = @gameplay.map.cell(scaledX, scaledY)
		
		case e.button

			when 1
			
				params = []
			
				if @state == @@STATE_MOVE
					params = [@guiObjects["MoveTroops"].value.to_i]
				end
			
				@gameplay.leftButton(region, params)
				
				#check turnstate and clickstate in gameplay to see what shape things are in
				#
				
				while not @gameplay.events.empty?
					event = @gameplay.events.pop
				
					
					#perform animation or IO operations based on which event was passed
					case event[0]
					
						when EVENT_TROOPS_PLACE
							puts "placed"
							@gameboard.queue_draw
							@guiObjects["StatusLBL"].markup = "<b>Player " + @gameplay.currentPlayer.to_s + ", Attack , Annex or Build.</b>"
						when EVENT_HARBOUR
							@guiObjects["StatusLBL"].markup = "<b>Player " + @gameplay.currentPlayer.to_s + ", Move your troops.</b>"
						
						when EVENT_ATTACK
							puts "attack"
							@gameboard.queue_draw
							@guiObjects["StatusLBL"].markup = "<b>Player " + @gameplay.currentPlayer.to_s + ", Move your troops.</b>"
						when EVENT_CONVERT
							puts "convert"
							@gameboard.queue_draw
						when EVENT_TROOPS_PREPARE_MOVE
							@state = @@STATE_MOVE
							@guiObjects["MoveTroops"].value = @gameplay.countries[region-1].troops
							puts "prepare move"
						when EVENT_TROOPS_MOVE
							puts "move"
							@gameboard.queue_draw
							@guiObjects["StatusLBL"].markup = "<b>Player " + @gameplay.currentPlayer.to_s + ", Place your reinforcements.</b>"
							
							
							#next playerColours
							#should player-advance be another event?
							@guiObjects["StatusLBL"].modify_fg( Gtk::STATE_NORMAL, Gdk::Color.new(65535, 65535, 65535) )
							
							case @gameplay.currentPlayer
								
								when 1 
									c = @guiObjects["p1c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
									
								when 2
									c = @guiObjects["p2c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
								
								when 3
									c = @guiObjects["p3c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
								
								when 4
									c = @guiObjects["p4c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
								
								when 5
									c = @guiObjects["p5c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
								
								when 6
									c = @guiObjects["p6c"].color
									@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(c.red, c.green, c.blue) )
									
							end
									
					
					end
				
				end

	
			when 3
	
				if region < WATERBODY_START
				
					name = @gameplay.countries[region-1].name
					defense = @gameplay. countries[region-1].troops.to_s
					
					@guiObjects["lblCountryName"].set_markup( "<b>" + name + "</b>" )
					@guiObjects["lblDefendStrength"].set_markup( "<big>" + defense + "</big>" )
					
				else
				
					name = @gameplay.lakes[region - WATERBODY_START - 1].name
					
					@guiObjects["lblCountryName"].set_markup( "<b>" + name + "</b>" )
					@guiObjects["lblDefendStrength"].set_markup( "<big>-</big>" )
					
				end
	
				
		end

	end
	
	def passResign
	
	end
	
	def p1colour
		c = @guiObjects["p1c"].color
		@gameboard.setPlayerColour(1, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end
	
	def p2colour
		c = @guiObjects["p2c"].color
		@gameboard.setPlayerColour(2, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end

	def p3colour
		c = @guiObjects["p3c"].color
		@gameboard.setPlayerColour(3, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end

	def p4colour
		c = @guiObjects["p4c"].color
		@gameboard.setPlayerColour(4, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end

	def p5colour
		c = @guiObjects["p5c"].color
		@gameboard.setPlayerColour(5, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end

	def p6colour
		c = @guiObjects["p6c"].color
		@gameboard.setPlayerColour(6, c.red/65535.0, c.green/65535.0, c.blue/65535.0)
		@gameboard.queue_draw
	end
	
	def newMap
	
	end
	
	
	def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
	  
		#Glade GUI stuff
		bindtextdomain(domain, localedir, nil, "UTF-8")
		@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler|method(handler)}
		
		@guiObjects = {
			"mainWindow" => @glade.get_widget("mainWindow"),
			"gameBoard" => @glade.get_widget("GameBoard"),
			"lblAttackStrength" => @glade.get_widget("lblAttackStrength"),
			"lblDefendStrength" => @glade.get_widget("lblDefendStrength"),
			"lblAttackData" => @glade.get_widget("lblAttackData"),
			"lblDefendData" => @glade.get_widget("lblDefendData"),
			"lblCountryName" => @glade.get_widget("lblCountryName"),
			"lblCountryOwner" => @glade.get_widget("lblCountryOwner"),
			"eventBoxBoard" => @glade.get_widget("EventBoxBoard"),
			"p1c" => @glade.get_widget("p1c"),
			"p2c" => @glade.get_widget("p2c"),
			"p3c" => @glade.get_widget("p3c"),
			"p4c" => @glade.get_widget("p4c"),
			"p5c" => @glade.get_widget("p5c"),
			"p6c" => @glade.get_widget("p6c"),
			"MoveTroops" => @glade.get_widget("MoveTroops"),
			"StatusLBL" => @glade.get_widget("StatusLBL"),
			"StatusPass" => @glade.get_widget("StatuPass"),
			"StatusEB" => @glade.get_widget("StatusEB")
			
		}
		

		
		
		map, countryCount, lakeCount = createMap(66, 45, 30, ISLAND_STYLE_SOME, 80, 70, 70, 20, 10)
		countryNames = createNames(countryCount, false)
		waterNames = createNames(lakeCount, true)
		troops = createTroopCounts(countryCount, countryCount, 20)
		
		@gameplay = Gameplay.new(map, countryCount, lakeCount, countryNames, waterNames, troops, 6)
		
		@guiObjects["mainWindow"].title = "OpenFracas"
		#gtkimg = Gdk::Pixbuf.new(drawMap(map, 1500), Gdk::Pixbuf::COLORSPACE_RGB, false, 8, map.width*10, map.height*10, map.width * 30)
		gtkimg = Gdk::Pixbuf.new("icon.png")
		@guiObjects["mainWindow"].icon = gtkimg
		
		@gameplay.countries[0].owner = 1
		@gameplay.countries[1].owner = 2
		@gameplay.countries[2].owner = 3
		@gameplay.countries[3].owner = 4
		@gameplay.countries[4].owner = 5
		@gameplay.countries[5].owner = 6
				
		#red colour in status bar
		@guiObjects["StatusEB"].modify_bg( Gtk::STATE_NORMAL, Gdk::Color.new(41942, 0, 0) )				
		@guiObjects["StatusLBL"].modify_fg( Gtk::STATE_NORMAL, Gdk::Color.new(65535, 65535, 65535) )
				
		@gameboard = GameBoard.new(@gameplay, @@GUI_SCALE)
		
		@gameboard.setPlayerColour(1, 0.64, 0.0, 0.0)
		@gameboard.setPlayerColour(2, 0.36, 0.21, 0.4)
		@gameboard.setPlayerColour(3, 0.305, 0.60, 0.01)
		@gameboard.setPlayerColour(4, 0.505, 0.325, 0.153)
		@gameboard.setPlayerColour(5, 0.671, 0.243, 0.569)
		@gameboard.setPlayerColour(6, 0.768, 0.627, 0.0)
		
		@gameboard.show

		
		@guiObjects["eventBoxBoard"].child = @gameboard
		#@guiObjects["gameBoard"].pixbuf = gtkimg
		@guiObjects["mainWindow"].show
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
		
		
		
		
		
		
		
		
		
		
		
	end

end


